package com.lianxi;


import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lianxi.dao.INewsDao;
import com.lianxi.dao.Pager;
import com.lianxi.es.EsHighServiceSearch;
import com.lianxi.es.Page;
import com.lianxi.pojo.Demo;
import com.lianxi.pojo.News;
import com.lianxi.pojo.User;
import com.lianxi.util.ESearchUtils;
import org.apache.commons.io.FilenameUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.script.mustache.SearchTemplateResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.jws.soap.SOAPBinding;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentType.JSON;

@Controller
public class TestController {

    private static final  String ESINDEX_TEST4="test4";

    @Autowired
    private EsHighServiceSearch esHighServiceSearch;

    @Autowired
    private RestHighLevelClient client;

//    @Autowired
    private INewsDao newsDao;

    @Autowired
    private OSSClient ossClient;

    @PostMapping("/test")
    public String test10(MultipartFile file) throws IOException {
//        GetIndexRequest request=new GetIndexRequest("test3");
//        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//        GetRequest getRequest=new GetRequest("test3","1");
//        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
//        Map<String, Object> source = documentFields.getSource();
        String originalFilename = file.getOriginalFilename();
        String oldName = file.getOriginalFilename();
        String ext = FilenameUtils.getExtension(oldName);
        ext = ext.toLowerCase();
        String s="hello";
        ossClient.putObject("ossorderqi","123",new ByteArrayInputStream(s.getBytes()));
        return "success";
    }

    @RequestMapping("/hello3")
    public String test3() throws IOException {
        News news = new News();
//        news.setTitle("23.34测试");
//        newsDao.add(news);
        News news1 = newsDao.get(90);

//        GetIndexRequest request=new GetIndexRequest("test3");
//        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//        GetRequest getRequest=new GetRequest("test3","1");
//        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
//        Map<String, Object> source = documentFields.getSource();
        return "success";
    }






    @RequestMapping("/hello")
    public String test(Page page) throws IOException {
        GetIndexRequest request=new GetIndexRequest("test3");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        GetRequest getRequest=new GetRequest("test3","1");
        GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
        Map<String, Object> source = documentFields.getSource();
        return "success";
    }

//    批量插入
    @RequestMapping("/add")
    @ResponseBody
    public String addSome(Page page) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        List<User> list=new ArrayList<>();
        User user = new User("齐浩",21);
        User user1 = new User("齐小浩",28);
        User user2 = new User("徐海琪",31);
        User user3 = new User("七娃",25);
        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        int i=0;
        for (User us : list) {
            bulkRequest.add(new IndexRequest("test4").id(""+(++i)).source(new ObjectMapper().writeValueAsBytes(us), JSON));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        bulk.hasFailures(); //false 表示没有失败
        return "success";
}

//查询
    @RequestMapping("/search")
    public String searchIndex() throws IOException {

        SearchRequest searchRequest = new SearchRequest(ESINDEX_TEST4);

        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();

        //查询条件使用QueryBuilders工具来实现
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("", "齐浩");
//        sourceBuilder.query(termQueryBuilder);
        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        ESearchUtils<User> userESearchUtils=new ESearchUtils<>();
        Pager<User> p2 = new Pager<>();
        p2.setLength(10);
        p2.setPage(1);
        userESearchUtils.searchUser("user","zhangsan",p2);
        Page page=new Page();
        page.setPageNumber(1);
        page.setPageSize(10);
        esHighServiceSearch.resp(page,search);
        SearchHits hits = search.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit);
        }



        return null;
    }

    //条件查询
    @RequestMapping("/searchby")
    public String searchIndex2() throws IOException {

        Page page=new Page();
        page.setPageNumber(1);
        page.setPageSize(2);
        List<User>  users= esHighServiceSearch.getListFromEs(ESINDEX_TEST4, new User(),page);
//        esHighServiceSearch.commonSearch(page,new String[]{ESINDEX_TEST4},new String[]{"：must::term::leader::齐浩"},new String[]{});

//
//        SearchRequest searchRequest = new SearchRequest(ESINDEX_TEST4);
//
//        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//
//        //查询条件使用QueryBuilders工具来实现
////        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("", "齐浩");
////        sourceBuilder.query(termQueryBuilder);
//        searchRequest.source(sourceBuilder);
//
//        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
//        Page page=new Page();
//        page.setPageNumber(1);
//        page.setPageSize(10);
//        esHighServiceSearch.resp(page,search);
//        SearchHits hits = search.getHits();
//        for (SearchHit hit : hits) {
//            System.out.println(hit);
//        }


        return null;
    }

}
